﻿<%@ Page Title="菜单管理" Language="C#"  AutoEventWireup="true" CodeBehind="Menu.aspx.cs" Inherits="Page_Menu" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>菜单管理</title>
    <link rel="stylesheet" type="text/css" href="../../css/theme.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/default/easyui.css"/>
	<link rel="stylesheet" type="text/css" href="../../css/icon.css"/>
    <script src="../../js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/Json.js" type="text/javascript"></script>
    <script src="../../js/Common.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">

    <div  id="container" style="width:100%;padding:5px" >
  
    
         <div id="toolbar" style="text-align:left ; padding-bottom:10px">  
             <a href="javascript:void(0)" class="easyui-linkbutton  l-btn"  id="add"  onclick="pageJs.addFolder()">新增目录</a>  
             <a href="javascript:void(0)" class="easyui-linkbutton  l-btn"  id="add2" onclick="pageJs.addReport()">新增菜单</a>  
             <a href="javascript:void(0)" class="easyui-linkbutton  l-btn"   id="edit" onclick="pageJs.edit()">编辑</a>  
             <a href="javascript:void(0)" class="easyui-linkbutton   l-btn"  id="del" onclick="pageJs.del()">删除</a>  
             <a href="javascript:void(0)" class="easyui-linkbutton   l-btn"   id="edit2"onclick="pageJs.up()">上移</a>  
             <a href="javascript:void(0)" class="easyui-linkbutton   l-btn"   id="edit3" onclick="pageJs.down()">下移</a>    
         </div> 
     
     
         <div id="divTable" style="width:100%; text-align:center ;" >
    <%--      border-top:solid 1px #95b8e7;--%>
             <div id="divTree" style="float:left;  text-align:left; width:200px;  overflow:auto;  ">
                <ul id="tree"></ul>  
         
             </div>
   
       
            <div id="divContent" class="easyui-panel" title="编辑区"  style="padding:10px 5px;background:#fafafa; overflow:hidden;"  > 
                 
                <div  id="divFolder" style=" display:none">
                    <div id="folderTitle" class="ftitle">目录明细</div>  

                        <div class="fitem">  
                            <label>目录名称:</label>  
                            <input name="folderName" id="folderName"   class="easyui-validatebox" disabled="disabled" />  
                            <input id="folderKey" type="hidden" value="0" />  
                        </div>

                        <div id="pnlFolder" style="padding-top:75px; width:250px; text-align:center; padding-left:20px; display:none ">  
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="pageJs.doSaveFolder()">保存</a>  
                            <a href="javascript:void(0)" id="unSaveFolder" class="easyui-linkbutton" iconCls="icon-cancel" onclick="pageJs.unSave()">取消</a>  
                        </div>  
                    </div>

                <div  id="divReport"  style=" display:none">
                    <div id="rptTitle" class="ftitle">菜单明细</div>  
  
                        <div class="fitem">  
                            <label>菜单名称:</label>  
                            <input name="rptName" id="rptName" class="easyui-validatebox" disabled="disabled" />  
                        </div>
                        <div class="fitem">  
                            <label>档案路径:</label>  
                            <input name="url" id="url"  style=" width:300px;ime-mode: disabled;" disabled="disabled"/>  
                            <input id="rptKey" type="hidden" value="0"  />  
                        </div>
                        <div id="pnlSave" style="padding-top:50px; width:250px; text-align:center;  padding-left:20px ;  display:none">  
                            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="pageJs.doSaveRpt()">保存</a>  
                            <a href="javascript:void(0)"  id="unSaveReport"  class="easyui-linkbutton" iconCls="icon-cancel" onclick="pageJs.unSave()">取消</a>  
                        </div>  
                    </div>

                     
            </div>  
     
      

     

         </div>


    </div>
    </form>
 <script type="text/javascript">

     var pageJs = {
         url: "",
         resize: function (h, w) {
             $('#container').height(h).width(w - 10);
             var contentH = h - $("#toolbar").outerHeight() - 10;
             if (contentH < 100) contentH = 100;
             $("#divTree").height(contentH);
             $("#divContent").height(contentH - $(".panel-header").outerHeight() - 20);
             var contentW = $("#divTable").width() - $("#divTree").outerWidth();
             $(".panel >div").width(contentW - 15);
         },

         currData: null,
         validateF: function () {
             var fname = $.trim($("#folderName").val());
             if (fname == "" || fname.length > 25)
                 return "目录名称为必须项，最长25字符！";
             return "";

         },
         validateR: function () {
             var fname = $.trim($("#rptName").val());
             var fpath = $.trim($("#url").val());
             if (fname == "" || fname.length > 25)
                 return "菜单名称为必须项，最长25字符！";
             if (fpath.length > 100)
                 return "档案路径最长50字符！";
             return "";

         },
         getFatherKey: function () {
             //获取父KEY
             //selected node data
             var pKey;
             if (pageJs.currData.attributes.FUNC_TYPE == 0) {
                 pKey = pageJs.currData.id;
             }
             else {
                 var node = $('#tree').tree('getSelected');
                 var parent = $('#tree').tree("getParent", node.target);
                 if (parent == null) return "";
                 var pData = $('#tree').tree("getData", parent.target);
                 pKey = pData.id;

             }
             return pKey;
         },
         getSequence: function () {
             //selected node data
             var seq;
             if (pageJs.currData.attributes.PARENT_KEY == "") return 1;
             if (pageJs.currData.attributes.FUNC_TYPE == 0) {
                 if (pageJs.currData.children.length) seq = pageJs.currData.children.length + 1;
                 else seq = 1;
             }
             else {
                 var node = $('#tree').tree('getSelected');
                 var parent = $('#tree').tree("getParent", node.target);
                 var pData = $('#tree').tree("getData", parent.target);
                 if (pData.children.length) seq = pData.children.length + 1;
                 else seq = 1;
             }
             return seq;
         },
         //保存目录(新增,编辑)
         doSaveFolder: function () {
             var valMsg = this.validateF();
             if (valMsg != "") {
                 BasicJs.showMessage(valMsg);
                 return;
             }
             var action = "add";
             var msg = "新增目录成功！";
             var folderKey = $("#folderKey").val();
             if (folderKey != "0") {
                 //edit
                 action = "edit";
                 msg = "保存目录成功！";
             }
             var parm = { id: folderKey, text: $.trim($("#folderName").val()), attributes: { SEQUENCE: this.getSequence(), PARENT_KEY: this.getFatherKey() }
             };

             $.post($.format("{0}?action={1}&type=folder", this.url, action), { data: JSON.stringify(parm) }, function (result) {
                 if (result.Success) {
                     BasicJs.showMessage(msg, true);
                     $("#pnlFolder").hide();
                     $("#folderName").attr("disabled", "disabled");
                     $('#tree').tree('reload');
                 }
                 else {
                     //error
                     BasicJs.showMessage(result.Message);
                 }

             }, "json"
             );

         },
         unSave: function () {
             var nodeSelect = $("#tree").tree("getSelected");
             $("#tree").tree("select", nodeSelect.target);
             $("#pnlSave,#pnlFolder").hide();
             $("#folderName,#rptName,#url").attr("disabled", "disabled");

         },


         doSaveRpt: function () {
             var valMsg = this.validateR();
             if (valMsg != "") {
                 BasicJs.showMessage(valMsg);
                 return;
             }
             var action = "add";
             var msg = "新增菜单成功！";
             var rptKey = $("#rptKey").val();
             if (rptKey != "0") {
                 //edit
                 action = "edit";
                 msg = "保存菜单成功！";
             }
             var parm = { id: rptKey, text: $.trim($("#rptName").val()), attributes: { SEQUENCE: this.getSequence(), PARENT_KEY: this.getFatherKey(), URL: $.trim($("#url").val()) }
             };
             $.post($.format("{0}?action={1}&type=rpt", this.url, action), { data: JSON.stringify(parm) }, function (result) {
                 if (result.Success) {
                     BasicJs.showMessage(msg, true);
                     $("#pnlSave").hide();
                     $("#rptName,#url").attr("disabled", "disabled");
                     $('#tree').tree('reload');
                 }
                 else {
                     //error
                     BasicJs.showMessage(result.Message);
                 }

             }, "json"
             );

         },


         initFolder: function () {
             $("#folderKey").val("0");
             $("#folderName").val("");
             $(".panel-title").html("新增目录");
             $("#divFolder").show();
             $("#divReport").hide();
             $("#unSaveFolder").show();

             $("#pnlFolder").show();
             $("#folderName").removeAttr("disabled").focus();
         },
         initReport: function () {
             $("#rptKey").val("0");
             $("#rptName").val("");
             $("#url").val("");
             $(".panel-title").html("新增菜单");
             $("#divReport").show();
             $("#divFolder").hide();
             $("#unSaveReport").show();
             $("#pnlSave").show();
             $("#rptName,#url").removeAttr("disabled");
             $("#rptName").focus();   
         },

         addFolder: function () {
             this.initFolder();

         },
         addReport: function () {
             this.initReport();
         },
         edit: function () {
             var nodeData = $('#tree').tree('getSelected');
             if (nodeData.attributes.FUNC_TYPE == 0) {
                 //编辑目录
                 $("#pnlFolder").show();
                 $("#folderName").removeAttr("disabled").focus();
             }
             else {
                 $("#pnlSave").show();
                 $("#rptName,#url").removeAttr("disabled");
                 $("#rptName").focus();   
             }
         },
         up: function () {
             var nodeData = $('#tree').tree('getSelected');
             var node = $('.tree-node-selected').parent();
             if (node.prev().length > 0) {
                 this.doSaveSequence(nodeData.attributes.SEQUENCE, nodeData.id, nodeData.attributes.PARENT_KEY, "up");
                 //  node.insertBefore(node.prev());
             }

         },
         down: function () {
             var nodeData = $('#tree').tree('getSelected');
             var node = $('.tree-node-selected').parent();
             if (node.next().length > 0) {
                 this.doSaveSequence(nodeData.attributes.SEQUENCE, nodeData.id, nodeData.attributes.PARENT_KEY, "down")
                 // node.insertAfter(node.next());
             }

         },
         doSaveSequence: function (sequence, key, father, action) {
             var parm = { id: key, attributes: { SEQUENCE: sequence, PARENT_KEY: father} };
             $.post(this.url + "?action=" + action, { data: JSON.stringify(parm) }, function (result) {
                 if (result.Success) {
                     $('#tree').tree('reload');
                 }
                 else {
                     //error
                     BasicJs.showMessage(result.Message);
                 }

             }, "json"
             );

         },

         del: function () {
             var node = $('#tree').tree('getSelected');
             var data = $('#tree').tree("getData", node.target);
             var obj = "菜单";
             if (data.attributes.FUNC_TYPE == 0) {
                 obj = "目录";
                 if (data.children.length && data.children.length > 0) {
                     BasicJs.showMessage("请先删除该目录下的菜单！");
                     return;
                 }
             }
             if (node) {

                 $.messager.confirm('删除' + obj, $.format('您确定要刪除选中的{0}吗?', obj), function (r) {
                     if (r) {

                         $.post(pageJs.url + "?action=del", { id: data.id, SEQUENCE: data.attributes.SEQUENCE, PARENT_KEY: data.attributes.PARENT_KEY, URL: data.attributes.RUL, FUNC_TYPE: data.attributes.FUNC_TYPE, FUNCTION_NAME: data.text }, function (result) {
                             if (result.Success) {
                                 pageJs.currID = null;
                                 $('#tree').tree('reload');    // reload the user data
                                 BasicJs.showMessage("刪除成功！", true);
                             } else {
                                 if (result.Message) BasicJs.showMessage(result.Message);
                             }
                         }, 'json');
                     }
                 });
             }
             else { BasicJs.showMessage("未选中要删除的节点！"); }


         },

         dataFilter: function (data) {
             return data;
         }


     }

     $(function () {
         //授权
         var funKey = window.name.substring(3);
         BasicJs.authorize("#add,#add2", funKey, "ADD");
         BasicJs.authorize("#edit,#edit2,#edit3", funKey, "EDIT");
         BasicJs.authorize("#del", funKey, "DELETE");
         $('#tree').tree({
             url: '../../ashx/System/hMenu.ashx?action=get',
             animate: true,

             onLoadSuccess: function(node, data) {
                 if ($("#tree").tree("getSelected") == null) {
                     var folder = eval(data[0].attributes.URL);
                     $.each(folder, function(index, item) {
                         var foNode = $("#tree").tree("find", item);
                         if (foNode && foNode.target) $(foNode.target).find(".tree-file").attr("class", "tree-folder");
                     });
                     if (pageJs.currID != null) { $("#tree").tree("select", $("#tree").tree("find", pageJs.currID).target); }
                     else {  $("#tree").tree("select", $("#tree").tree("getRoot").target);  }
                 }

             },
             //选择
             onSelect: function(node) {
                 var data = $('#tree').tree("getData", node.target);
                 pageJs.currData = data;
                 pageJs.currID = data.id;
                 if (data.attributes.FUNC_TYPE == 0 ) {
                     $("#folderKey").val(data.id);
                     $("#folderName").val(data.text);
                     $("#pnlFolder").hide();
                     $("#folderName").attr("disabled", "disabled");
                     $(".panel-title").html("编辑目录");
                     $("#divFolder").show();
                     $("#divReport").hide();
                     $("#pnlFolder").hide();
                   //  setTimeout("$(\"#folderName\").focus()", 100);
                 }
                 else {
                     $("#rptKey").val(data.id);
                     $("#rptName").val(data.text);
                     $("#url").val(data.attributes.URL);
                     $("#pnlSave").hide();
                     $("#rptName,#url").attr("disabled", "disabled");
                     $(".panel-title").html("编辑菜单");
                     $("#divReport").show();
                     $("#divFolder").hide();
                     $("#pnlSave").hide();
                   //  setTimeout("$(\"#rptName\").focus()", 100);
                 }

             },
             loadFilter: function(data) {
                 if (data.d) {
                     return data.d;
                 } else {
                     return data;
                 }
             }
         });
         pageJs.resize($(window).height(),$(window).width());
         pageJs.url = '../../ashx/System/hMenu.ashx';

     });  
        
       
    
     </script>  
     <style type="text/css">   
         .ftitle{  
            font-size:14px;  
           font-weight:bold;  
           padding:5px 0;  
             margin-bottom:10px;  
             border-bottom:1px solid #ccc;  
        }  
        .fitem{  
           margin-bottom:5px;  
           margin-left :20px;
        }  
        .fitem label{  
             display:inline-block;  
            width:80px;  
         } 
          .searchBlock 
          {
              padding-left:20px;
              
              }
                .searchBlock span
          {
              
              
              }

          .searchTitle
         {
              padding-right:2px;
             }
     </style> 


</body>
</html>

